package com.shujia;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
import org.apache.hadoop.hbase.mapreduce.SimpleTotalOrderPartitioner;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo9BulkLoading {
    public static class BulkLoadingMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");
            String mdn = splits[0];
            String start_time = splits[1];
            String rowkey = mdn + "_" + start_time;
            String end_time = splits[2];
            String lg = splits[4];
            String lat = splits[5];
            // 列簇
            byte[] cf1 = "cf1".getBytes();
            // 构建KeyValue对象，相当于一个Cell
            KeyValue et = new KeyValue(rowkey.getBytes(), cf1, "et".getBytes(), end_time.getBytes());
            KeyValue longitude = new KeyValue(rowkey.getBytes(), cf1, "lg".getBytes(), lg.getBytes());
            KeyValue latitude = new KeyValue(rowkey.getBytes(), cf1, "lat".getBytes(), lat.getBytes());

            // 将三个KeyValue对象依次输出
            context.write(new ImmutableBytesWritable(rowkey.getBytes()), et);
            context.write(new ImmutableBytesWritable(rowkey.getBytes()), longitude);
            context.write(new ImmutableBytesWritable(rowkey.getBytes()), latitude);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
        Job job = Job.getInstance(conf);
        job.setJobName("Demo9BulkLoading");
        job.setJarByClass(Demo9BulkLoading.class);

        // 保证每个Reduce之间是有序的
        job.setPartitionerClass(SimpleTotalOrderPartitioner.class);

        // 配置Map任务
        job.setMapperClass(BulkLoadingMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        // 配置Reduce任务
        // 会将进入reduce中的数据进行排序以符合HBase的要求
        job.setReducerClass(KeyValueSortReducer.class);

        // 配置4个reduce任务
        job.setNumReduceTasks(4);

        // 配置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/dianxin"));
        FileOutputFormat.setOutputPath(job, new Path("/data/hfile"));

        // 对MR的输出按照HBase的要求进行格式化
        Connection conn = ConnectionFactory.createConnection(conf);
        Table bulk_load = conn.getTable(TableName.valueOf("bulk_load"));
        RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("bulk_load"));
        HFileOutputFormat2.configureIncrementalLoad(job,bulk_load,regionLocator);

        job.waitForCompletion(true);

        // 将生成的HFile加载到HBase中
        LoadIncrementalHFiles load = new LoadIncrementalHFiles(conf);
        load.doBulkLoad(new Path("/data/hfile"),conn.getAdmin(),bulk_load,regionLocator);


        /**
         * create 'bulk_load','cf1'
         * hadoop jar HBase-1.0-jar-with-dependencies.jar com.shujia.Demo9BulkLoading
         */
    }
}
